import requests

class TpshopLoginApi(object):
    # 创建Session实例
    session = requests.Session()

    # 发送验证码请求
    @classmethod
    def get_verify(cls):
        # 使用Session实例调用get方法，发送获取验证码请求(不需要获取cookie)
        cls.session.get("http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=verify&r=0.7345651820241239")

    # 发送登录请求
    @classmethod
    def login(cls, login_data):
        # 使用同一个Session实例post发送登录请求url、请求头、请求体  不需要携带cookie，得到响应结果
        url = "http://hmshop-test.itheima.net/index.php?m=Home&c=User&a=do_login&t=0.09314633930127925"
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        resp = cls.session.post(url=url, headers=headers, data=login_data)
        return resp

# if __name__ == '__main__':
#     TpshopLoginApi.get_verify()
#     login_data = {
#         "username": "18320692379",
#         "password": "l141027",
#         "verify_code": "8888"
#     }
#     resp = TpshopLoginApi.login(login_data)
#     print(resp.json())